<template>
  <section class="tag-wrapper">
    <el-tag
      :disable-transitions="true"
      :key="index"
      @close="handleClose(tag,index)"
      closable
      v-for="(tag, index) in tagsList"
    >{{ tag.name }}</el-tag>
    <el-input
      @blur="handleInputConfirm"
      @keyup.enter.native="handleInputConfirm"
      class="input-new-tag"
      ref="saveTagInput"
      size="small"
      v-if="inputVisible"
      v-model="inputValue"
    />
    <el-button @click="showInput" class="button-new-tag" size="small" v-else>+ New Tag</el-button>
  </section>
</template>

<script>
export default {
  props: {
    tagsList: {
      type: [Array, String],
      required: true,
    },
  },
  data() {
    return {
      inputVisible: false,
      inputValue: '',
    };
  },
  computed: {},
  methods: {
    // 关闭处理器
    handleClose(index) {
      // this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      // 向父组件抛出delete事件
      this.$emit('delete', index);
    },
    // 显示输入框
    showInput() {
      this.inputVisible = true;
      // 自动聚焦输入框
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    // 提交输入内容
    handleInputConfirm() {
      const { inputValue } = this;
      if (inputValue) {
        // this.dynamicTags.push(inputValue);
        // 向父组件抛出创建标签事件
        this.$emit('create', inputValue);
      }
      // 隐藏输入框
      this.inputVisible = false;
      // 情况输入框内容
      this.inputValue = '';
    },
  },
};
</script>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
